/*
 * Copyright 2012 Dirk Vranckaert
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package eu.vranckaert.utility.activity.abcompat.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import eu.vranckaert.utility.activity.abcompat.helper.ActionBarHelper;

/**
 * Date: 16/10/12
 * Time: 19:00
 *
 * Adds fragment support and keeps the ActionBar functionality. But the {@link FragmentActivity} it extends is basically
 * a {@link com.google.android.maps.MapActivity} because otherwise no {@link com.google.android.maps.MapView} can be
 * used in the fragments. This is due to lack of map-support in the android v4 support library. The library used instead
 * is coming from here: https://github.com/petedoyle/android-support-v4-googlemaps/ and only adds the map support to the
 * support library.
 *
 * @author Dirk Vranckaert
 */
public abstract class ActionBarFragmentActivity extends FragmentActivity {
    final ActionBarHelper mActionBarHelper = ActionBarHelper.createInstance(ActionBarFragmentActivity.this);

    /**
     * Returns the {@link ActionBarHelper} for this ui.
     */
    protected ActionBarHelper getActionBarHelper() {
        return mActionBarHelper;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MenuInflater getMenuInflater() {
        return mActionBarHelper.getMenuInflater(super.getMenuInflater());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBarHelper.onCreate(savedInstanceState);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mActionBarHelper.onPostCreate(savedInstanceState);
    }

    /**
     * Base action bar-aware implementation for
     * {@link android.app.Activity#onCreateOptionsMenu(android.view.Menu)}.
     * <p/>
     * Note: marking menu items as invisible/visible is not currently supported.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean retValue = false;
        retValue |= mActionBarHelper.onCreateOptionsMenu(menu);
        retValue |= super.onCreateOptionsMenu(menu);
        return retValue;
    }

    /**
     * Base action bar-aware implementation for
     * {@link android.app.Activity#onPrepareOptionsMenu(android.view.Menu)}.<br/>
     * This method is only called when a device's menu button is pressed and will, just as the
     * {@link android.app.Activity#onCreateOptionsMenu(android.view.Menu)}, hide the elements in the menu that are
     * already visible in the action-bar.
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return mActionBarHelper.onCreateOptionsMenu(menu);
    }

    /**
     * Method to be called if you want the menu to be refreshed (if you want to update the items shown for some reason).
     * On all devices after {@link android.os.Build.VERSION#SDK_INT} 11 the {@link android.app.Activity#invalidateOptionsMenu()} is
     * called which will re-invoke the {@link android.app.Activity#onCreateOptionsMenu(android.view.Menu)}.<br/>
     * On all devices before SDK 11 the method {@link android.app.Activity#onPrepareOptionsMenu(android.view.Menu)} will
     * be invoked. So when using this method both the onPrepareOptionsMenu should be used and the onCreateOptionsMenu.
     * In there you should have some logic that decides which menu-resource-file to be set in what case.
     * <p>
     * <b>BUT MORE IMPORTANT</b> when using this method is that the onPrepareOptionsMenu method should look something
     * like this:
     * <pre>
     * {@code
     * @Override
     * public boolean onPrepareOptionsMenu(Menu menu) {
     *     if (Build.VERSION.SDK_INT < 11) {
     *         menu.clear();
     *         MenuInflater menuInflater = getMenuInflater();
     *         int menuResId;
     *         // YOUR CODE TO GET THE CORRECT MENU
     *         menuInflater.inflate(menuResId, menu);
     *         return super.onPrepareOptionsMenu(menu);
     *     }
     *     return true;
     * }
     * }
     * </pre>
     * </p>
     * <p>
     * <b>AND</b> for the onCreateOptionsMenu it should look something like this:
     * <pre>
     * {@code
     * @Override
     * public boolean onCreateOptionsMenu(Menu menu) {
     *     if (Build.VERSION.SDK_INT >= 11) {
     *         menu.clear();
     *         MenuInflater menuInflater = getMenuInflater();
     *         int menuResId;
     *         // YOUR CODE TO GET THE CORRECT MENU
     *         menuInflater.inflate(menuResId, menu);
     *     }
     *     // Before returning the super-call you can do other stuff like
     *     // setting the homeButton enabled/disabled...
     *     return super.onCreateOptionsMenu(menu);
     * }
     * }
     * </pre>
     * </p>
     */
    public void refreshMenu() {
        if (Build.VERSION.SDK_INT >= 11) {
            invalidateOptionsMenu(); // Will trigger an update of the options menu, but only exists since api 11.
        } else {
            getActionBarHelper().updateActionBarMenuChange();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        mActionBarHelper.onTitleChanged(title, color);
        super.onTitleChanged(title, color);
    }

    /**
     * Enables or disables the home button to be used for up-navigation.
     *
     * @param upEnabled If the home button should be enabled as up-navigation or not.
     */
    public void setDisplayHomeAsUpEnabled(boolean upEnabled) {
        mActionBarHelper.setDisplayHomeAsUpEnabled(upEnabled);
    }

    /**
     * Enables or disables the home button.
     *
     * @param enabled If the home button should be enabled or not.
     */
    public void setHomeButtonEnabled(boolean enabled) {
        mActionBarHelper.setHomeButtonEnabled(enabled);
    }
}
